<?php
/**
  * UserController.php
  *
  * This is not a freeware, use is subject to license terms
  * @author CLOUD DEV TEAM
  * 
  */
if ( !defined( "IN_CLOUD" ) ) exit ( 'Access Denied!' );

class UserController extends Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->checkLogin();
	}
	
	/**
	 * 验证是否已登录
	 */
	public function checkLogin()
	{
		if ( !$this->userInfo['userId'] || !$this->userInfo['userName'] )
		{
			Url::showMsg( '对不起，您还没有登录，请先登录' , '?c=login' );
		}
	}
	
	public function run()
	{
		Url::redirectUrl( SITEURL . '/?c=user&a=profile' );
	}
	
	/**
	 * 修改用户资料
	 */
	public function profile()
	{
		$userModel = User::getInstance();
		$userInfo = $userModel->getOne( array( 'uid' => $this->userInfo['userId'] ) );
		
		if ( $this->post['dosubmit'] )
		{
			$nickName = trim( Validation::filterData( $this->post['nickname'] ) );
			$realName = trim( Validation::filterData( $this->post['realname'] ) );
			$email = trim( Validation::filterData( $this->post['email'] ) );
			$birthday = trim( Validation::filterData( $this->post['birthday'] ) );
			
			if ( empty( $nickName ) )
			{
				Url::showMsg( '对不起，昵称不能为空' );
			}
			
			if ( empty( $email ) )
			{
				Url::showMsg( '对不起，邮箱不能为空' );
			}
			elseif ( !User::validateEmail( $email ) )
			{
				Url::showMsg( '对不起，邮箱无效' );
			}
			
			
			
			$data = array(
				'nickname' => $nickName,
				'realname' => $realName,
				'email' => $email,
				'birthday' => $birthday,
			);
			
			if ( $userModel->update( $data , array( 'uid' => $this->userInfo['userId'] ) ) )
			{
				Url::showMsg( '修改用户资料成功' , HTTP_REFERER );
			}
			else 
			{
				Url::showMsg( '修改用户资料失败' );
			}
			
		}
		
		$this->viewModel->tplAssign( 
			array(
				'userInfo',
			) , 
			array(
				$userInfo,
			) 
		);

		$this->viewModel->display( 'profile.htm' );
	}
	
	/**
	 * 修改密码
	 */
	public function modifyPassword()
	{
		$userModel = User::getInstance();
		$userInfo = $userModel->getOne( array( 'uid' => $this->userInfo['userId'] ) );
		
		if ( $this->post['dosubmit'] )
		{
			$oldPassword = trim( $this->post['oldpassword'] );
			$newPassword = trim( $this->post['newpassword'] );
			$confirmNewPassword = trim( $this->post['confirm_newpassword'] );
			
			if ( $oldPassword )
			{
				if ( strlen( $newPassword ) < 6 || strlen( $confirmNewPassword ) < 6 )
				{
					Url::showMsg( '对不起，密码太短了' );
				}
				elseif ( $newPassword != $confirmNewPassword )
				{
					Url::showMsg( '对不起，两次输入密码不一致' );
				}
				elseif ( $userInfo['password_md'] != md5( $oldPassword ) )
				{
					Url::showMsg( '对不起，原始密码不正确' );
				}
			}
			
			if ( $oldPassword )
			{
				$data = array(
					'password_md' => md5( $newPassword ),
				);
			}
			
			if ( $userModel->update( $data , array( 'uid' => $this->userInfo['userId'] ) ) )
			{
				Url::showMsg( '修改用户密码成功' , HTTP_REFERER );
			}
			else 
			{
				Url::showMsg( '修改用户密码失败' );
			}
			
		}
		$this->viewModel->tplAssign( 
			array(
				'userInfo',
			) , 
			array(
				$userInfo,
			) 
		);

		$this->viewModel->display( 'modifyPassword.htm' );
	}
	
	/**
	 * 设置默认图片
	 */
	public function setDefaultPic()
	{
		$id = ( int )$this->post['id'];
		
		if ( $id < 1 )
		{
			exit ( '1|参数错误' );
		}
		
		$pictureModel = Picture::getInstance();
		$pictureInfo = $pictureModel->getOne( array( 'pid' => $id ) );
		
		if ( !$pictureInfo )
		{
			exit( '2|此图片不存在' );
		}
		elseif ( $pictureInfo['uid'] != $this->userInfo['userId'] )
		{
			exit( '3|对不起，您没有权限设置此图片' );
		}
		
		$userModel = User::getInstance();
		if ( $userModel->update( array( 'picture_address' => $pictureInfo['picture_address'] ) , array( 'uid' => $this->userInfo['userId'] ) ) )
		{
			exit( '0|成功' );
		}
		else
		{
			exit( '4|设置默认图片错误' );
		}
		
	}
	
	/**
	 * ajax检测用户名是否合法
	 * @return 1:不合法,2:该用户名已被注册,3:可以注册
	 */
	public function ajaxCheckUserName()
	{
		$userName = trim( $this->post['user_name'] );
		if ( empty( $userName ) )
		{
			exit( '1' );
		}
		if ( User::validateUserName( $userName ) )
		{
			$userModel = User::getInstance();
			$user = $userModel->getOne( array( 'user_name' => $userName ) );
			if ( $user )
			{
				exit( '2' );
			}
			else
			{
				exit( '3' );
			}
		}
		else
		{
			exit( '1' );
		}
		
	}
	
	/**
	 * ajax检测邮箱是否合法
	 */
	public function ajaxCheckEmail()
	{
		$email = trim( $this->post['user_name'] );
		if ( empty( $email ) )
		{
			exit( 1 );
		}
		if ( User::validateEmail( $email ) )
		{
			exit( 3 );
		}
		else
		{
			exit( 1 );
		}
	}
	
	/**
	 * 登出
	 */
	public function logout()
	{
		Session::set( 'userId' , '' );
		Session::set( 'userName' , '' );
		Session::set( 'nickName' , '' );
		
		Url::showMsg( '您已成功登出' , SITEURL );
	}
}